package cn.tianyuhan.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.core.util.StrUtil;

import java.io.File;
import java.util.Optional;

public class MediaConvertUtil {
    static {
        File file = new File("/usr/bin/mediaconvert");
        FileUtil.writeFromStream(
                FileHandleUtil.class.getClassLoader().getResourceAsStream("mediaconvert"),
                file);
        file.setExecutable(true,false);
    }

    private static final String MEDIACONVERT = "mediaconvert";
    private static final String INPUT = "-i";
    private static final String TRANS = "-t";
    private static final String WAV = "wav";
    private static final String OUTPUT = "-o";

    /***
     * @Description 音频转换
     * @param sourcePath
     * @param targetPath
     * @return boolean 是否转换成功
     */
    public static boolean trans(String sourcePath, String targetPath) {
        String execResult = RuntimeUtil.execForStr(
                MEDIACONVERT,
                INPUT, sourcePath,
                TRANS, WAV,
                OUTPUT, targetPath
        );
        if (!execResult.contains("success")) {
            throw new RuntimeException(StrUtil.format("转换失败:{}", execResult));
        } else {
            return true;
        }
    }
}
